package sax.representation;

import java.util.Vector;

import sax.utils.Settings;

/**
 * Class which represents time series of Image
 * @author Krzysztof Figaj
 *
 */
public class TimeSeries {

	private Vector<Double> timeSeries;
	
	public TimeSeries() {
		timeSeries = new Vector<Double>();
		timeSeries.ensureCapacity(Settings.TIME_SERIES_NUMBER);
	}
	
	public Vector<Double> getTimeSeriesVector(){
		return timeSeries;
	}
	
	public Object[] getTimeSeriesArray(){
		return timeSeries.toArray();
	}
	
	public void add(Double value){
		timeSeries.add(value);
	}
	
	public static TimeSeries normalize(TimeSeries timeSeries){
		double meanValue = 0;	
		for(Double i : timeSeries.getTimeSeriesVector())
			meanValue += i;
		
		meanValue /= timeSeries.getTimeSeriesVector().size();
		
		double standardDeviation = 0;	
		boolean isRound = true;
		for(Double i : timeSeries.getTimeSeriesVector()){
			standardDeviation += Math.pow(i - meanValue, 2);
			if(isRound && i - meanValue > Settings.THRESHOLD)
				isRound = false;
		}
		
		if(isRound) // do not normalize round shapes
			return timeSeries;
		
		standardDeviation /= timeSeries.getTimeSeriesVector().size()-1;
		standardDeviation = Math.sqrt(standardDeviation);
		
		TimeSeries newTimeSeries = new TimeSeries();
		newTimeSeries.getTimeSeriesVector().ensureCapacity(
				timeSeries.getTimeSeriesVector().capacity());
		for(Double i : timeSeries.getTimeSeriesVector())
			newTimeSeries.add( (i - meanValue)/standardDeviation );
		
				
		return newTimeSeries;
	}
	
	public static TimeSeries reduce(TimeSeries timeSeries, int numberOfElements){
		if(numberOfElements < timeSeries.getTimeSeriesVector().size()){
			int step = timeSeries.getTimeSeriesVector().size()/numberOfElements;
			TimeSeries newTimeSeries = new TimeSeries();
			for(int i=0, y=0; i<timeSeries.getTimeSeriesVector().size() &&
			y< numberOfElements; i += step, y++)
				newTimeSeries.getTimeSeriesVector().add(
						timeSeries.getTimeSeriesVector().get(i));
			
			return newTimeSeries;
		} else
			return timeSeries;
	}
}
